import { createStore} from '../redux';
import combinedReducer from './reducers'
// import logger from './logger'
// import thunk from './thunk'
// import promise from './promise'
// import {applyMiddleware} from '../redux'


function promise({getState,dispatch}){
    return function(next){//为了实现中间件的级联，调用下一个中间件
      return function(action){//这才就是我们改造后的dispatch方法了
        if(action.then && typeof action.then === 'function'){
          return action.then(dispatch)
        }
        next(action)
      }
    }
}
function applyMiddleware(promise){
    return function(createStore){
        return function(reducer,preloadedState){
            let store = createStore(reducer,preloadedState);
            let dispatch //它会指向改造后的dispatch，这时dispatch是undefined
            // middlewareAPI.dispatch 最后指向的就是改造后的dispatch，因为有些时候希望能够嵌套或者说重新派发
            let middlewareAPI = {
                getState:store.getState,
                dispatch:(action)=>dispatch(action)
            } 
            dispatch = promise(middlewareAPI)(store.dispatch);
            return {
                ...store,
                dispatch
            };
        }
    }
}
const store = applyMiddleware(promise)(createStore)(combinedReducer,{counter1:{number:0},counter2:{number:0}})
export default store



// import { createStore} from '../redux';
// import combinedReducer from './reducers'
// // import logger from './logger'
// // import thunk from './thunk'
// // import {applyMiddleware} from '../redux'

// function thunk({getState,dispatch}){
//     return function(next){//为了实现中间件的级联，调用下一个中间件
//       return function(action){//这才就是我们改造后的dispatch方法了
//         if(typeof action === 'function'){
//             // 执行此函数，并且传入getState,dispatch
//             return action(getState,dispatch)
//         }
//         // 执行原始的store.dispatch
//         return next(action);
//       }
//     }
// }

// function applyMiddleware(thunk){
//     return function(createStore){
//         return function(reducer,preloadedState){
//             let store = createStore(reducer,preloadedState);
//             let dispatch //它会指向改造后的dispatch，这时dispatch是undefined
//             // middlewareAPI.dispatch 最后指向的就是改造后的dispatch，因为有些时候希望能够嵌套或者说重新派发
//             let middlewareAPI = {
//                 getState:store.getState,
//                 dispatch:(action)=>dispatch(action)
//             }
//             dispatch = thunk(middlewareAPI)(store.dispatch);
//             return {
//                 ...store,
//                 dispatch
//             };
//         }
//     }
// }
// const store = applyMiddleware(thunk)(createStore)(combinedReducer,{counter1:{number:0},counter2:{number:0}})
// export default store



// import { createStore} from '../redux';
// import combinedReducer from './reducers'
// // import logger from './logger'
// // import {applyMiddleware} from '../redux'

// /**
//  * 中间件的核心原理就是重写 store.dispatch 
//  * 不管是什么中间件，不管是什么逻辑，他的基本架构都是一样的
//  * getState 获取仓库中的状态
//  * dispatch 重新派发动作
//  * next 调用下一个中间件或者原始的store.dispatch
//  */
// function logger({getState,dispatch}){
//     return function(next){//为了实现中间件的级联，调用下一个中间件
//       return function(action){//这才就是我们改造后的dispatch方法了
//           console.log('prev state',getState());
//           next(action);//如果你只有一个中间件的话，next就是原始的store.dispatch(action)
//           console.log('next state',getState());
//           return action;
//       }
//     }
//  }
// function applyMiddleware(logger){
//     return function(createStore){
//         return function(reducer,preloadedState){
//             let store = createStore(reducer,preloadedState);
//             let dispatch //它会指向改造后的dispatch，这时dispatch是undefined
//             // middlewareAPI.dispatch 最后指向的就是改造后的dispatch，因为有些时候希望能够嵌套或者说重新派发
//             let middlewareAPI = {
//                 getState:store.getState,
//                 dispatch:(action)=>dispatch(action)
//             }
//             dispatch = logger(middlewareAPI)(store.dispatch);
//             return {
//                 ...store,
//                 dispatch
//             };
//         }
//     }
// }
// const store = applyMiddleware(logger)(createStore)(combinedReducer,{counter1:{number:0},counter2:{number:0}})
// export default store


// import { createStore} from '../redux';
// import combinedReducer from './reducers'

// let store = createStore(combinedReducer);
// 先缓存老的disatch方法
// let oldDispatch = store.dispatch;

// 中间件的核心原理就是重写 store.dispatch 
// store.dispatch = function (action) {
//     setTimeout(() => {
//         oldDispatch(action);
//     }, 1000);
//     return action;
// };
/**
store.dispatch = function (action) {
    console.log('prev state',store.getState());
    oldDispatch(action);
    console.log('new state',store.getState());
    return action;
};
 */


// export default store